package com.itfreer.data.report;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.itfreer.utils.ResponseEntity;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 统计报表基本rest服务 注:目前只支持单表统计。
 * 
 * @author cai
 *
 * @param <T>
 */
public abstract class RestReportService<T> {

	protected abstract BaseReportService<T> getService();

	@SuppressWarnings({ "rawtypes" })
	private Class getTClass() {
		Class entityClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		return entityClass;
	}

	@SuppressWarnings("unchecked")
	private T getNewT() {
		T jEntity = null;
		try {
			jEntity = (T) getTClass().newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return jEntity;
	}

	/**
	 * 获取统计数据
	 * 
	 * @param where
	 *            查询条件
	 * @return
	 * @throws IOException
	 */
	@ApiOperation(value = "获取统计数据")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "where", value = "查询参数", paramType = "body", required = true) })
	@PostMapping(value = "/getReports", produces = "application/json")
	public ResponseEntity<List<?>> getReports(@RequestBody Map<String, Object> where) throws IOException {
		T entity = this.getNewT();
		return new ResponseEntity<>(getService().getReports(entity, where, "", ""));
	}
}
